<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js类之属性访问器</title>
</head>
<body>
  <ul>
    <li>可在class中使用访问器，以达到对数据的合法性验证</li>
  </ul>
  <script>
    class Request {
      constructor(host) {
        this._data = {}
        this.host = host
      }
      get host() {
        return this._data.host
      }
      set host(val) {
        if (!/^https?:\/\//.test(val)) {
          throw new Error('host必须以http(s)://开头')
        }
        this._data.host = val
      }
    }
    // const req = new Request('github.com/miracle-git')
    const req = new Request('https://github.com/miracle-git')
    console.log(req.host)  // https://github.com/miracle-git
  </script>
</body>
</html>